package com.chwn.curiousdiary.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.download.DownHelper;
import com.chwn.curiousdiary.gloable.UserUtils;
import com.chwn.curiousdiary.utils.ImgLoadUtil;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.base.CommonBaseAdapter;

import java.util.List;

/**
 * Created by Myshikin on 2017/3/22.
 * desc ：故事列表
 */
public class CollectionAdapter extends CommonBaseAdapter<StoryBean> {

    private int errorValue;

    public CollectionAdapter(Context context, List<StoryBean> datas, boolean isOpenLoadMore) {
        super(context, datas, isOpenLoadMore);
    }

    @Override
    protected void convert(ViewHolder holder, final StoryBean data) {
        final int position = mDatas.indexOf(data);
        errorValue = holder.getAdapterPosition() - position;
        ImageView mIVStory = holder.getView(R.id.iv_story_img);
        holder.setText(R.id.tv_story_name, data.getStoryTitle());
        holder.setText(R.id.tv_story_desc, data.getStoryDetailTitle());
        ImgLoadUtil.displayStoryImage(mIVStory, data.getStoryImage());

        holder.setText(R.id.tv_left, data.getStoryListenCountString());
        holder.setText(R.id.tv_right, data.getStoryCreateTimeString());

        ImageView downState = holder.getView(R.id.iv_download_state);
        data.setDownState(data.getDownState() == 1 ? 1 : DownHelper.isDown(mContext, data.getStoryId()) ? 1 : 0);
        downState.setImageResource(data.getDownState() == 0 ? R.drawable.icon_down_orange : R.drawable.icon_downed_orange);


        downState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserUtils.isLogin(mContext)) {
                    return ;
                }
                if (data.getDownState() == 0) {
                    DownHelper.downStory(mContext, data);
                    data.setDownState(1);
                    notifyItemChanged(position + errorValue ,data);
                }
            }
        });

    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_collect_story;
    }
}
